library(stringr)
count_bar_generate_y <- function(vec_temp = c()){
  vec_temp <- as.character(vec_temp)
  for(i in 1:length(vec_temp)){
    if(is.na(vec_temp[i])){
      vec_temp[i] <- "NA Inputs"
    }
    if(is.null(vec_temp[i])){
      vec_temp[i] <- "NULL Inputs"
    }
  }
  type_vec <- unique(vec_temp)
  count_vec <- rep(0, length(type_vec))
  for(i in 1:length(type_vec)){
    count_vec[i] <- length(vec_temp[vec_temp == type_vec[i]])
  }
  out_df <- data.frame(type_vec, count_vec)
  names(out_df) <- c("types", "count")
  return(out_df)
}

mean_bar_generate_y <- function(vec_temp = c(), num_temp = c()){
  vec_temp <- as.character(vec_temp)
  for(i in 1:length(vec_temp)){
    if(is.na(vec_temp[i])){
      vec_temp[i] <- "NA Inputs"
    }
    if(is.null(vec_temp[i])){
      vec_temp[i] <- "NULL Inputs"
    }
  }
  type_vec <- unique(vec_temp)
  count_vec <- rep(0, length(type_vec))
  mean_vec <- rep(0, length(type_vec))
  median_vec <- rep(0, length(type_vec))
  sd_vec <- rep(0, length(type_vec))
  min_vec <- rep(0, length(type_vec))
  max_vec <- rep(0, length(type_vec))
  q25_vec <- rep(0, length(type_vec))
  q75_vec <- rep(0, length(type_vec))
  num_temp <- as.numeric(num_temp)
  for(i in 1:length(type_vec)){
    count_vec[i] <- length(vec_temp[vec_temp == type_vec[i]])
    mean_vec[i] <- mean(num_temp[vec_temp == type_vec[i]], na.rm = T)
    median_vec[i] <- median(num_temp[vec_temp == type_vec[i]], na.rm = T)
    sd_vec[i] <- sd(num_temp[vec_temp == type_vec[i]], na.rm = T)
    min_vec[i] <- min(num_temp[vec_temp == type_vec[i]], na.rm = T)
    max_vec[i] <- max(num_temp[vec_temp == type_vec[i]], na.rm = T)
    q25_vec[i] <- quantile(num_temp[vec_temp == type_vec[i]], na.rm = T)[2]
    q75_vec[i] <- quantile(num_temp[vec_temp == type_vec[i]], na.rm = T)[4]
  }
  out_df <- data.frame(type_vec, count_vec, mean_vec, median_vec, sd_vec, min_vec, max_vec, q25_vec, q75_vec)
  names(out_df) <- c("types", "count", "mean", "median", "sd", "min", "max", "q25", "q75")
  return(out_df)
}

count_bar_generate_y_weighted <- function(vec_temp = c(), weight_temp = c(), na.rm = T){
  vec_temp <- as.character(vec_temp)
  for(i in 1:length(vec_temp)){
    if(is.na(vec_temp[i])){
      vec_temp[i] <- "NA Inputs"
    }
    if(is.null(vec_temp[i])){
      vec_temp[i] <- "NULL Inputs"
    }
  }
  type_vec <- unique(vec_temp)
  count_vec <- rep(0, length(type_vec))
  for(i in 1:length(type_vec)){
    count_vec[i] <- sum(weight_temp[vec_temp == type_vec[i]], na.rm = na.rm)
  }
  out_df <- data.frame(type_vec, count_vec)
  names(out_df) <- c("types", "count")
  return(out_df)
}



cbargy_multi <- function(data = data.frame(), vec_names = c(), sepper = "lulululu"){
  new_vec <- rep("", dim(data)[1])
  for(i in 1:length(vec_names)){
    new_vec <- paste(new_vec, data[,vec_names[i]], sep = sepper)
  }
  out_data <- count_bar_generate_y(vec_temp = new_vec)
  for(i in 1:length(vec_names)){
    out_data[,vec_names[i]] <- ""
  }
  for(i in 1:dim(out_data)[1]){
    temp <- str_split(out_data$types[i], pattern = sepper)[[1]]
    for(j in 1:length(vec_names)){
      out_data[i,vec_names[j]] <- temp[j + 1]
    }
  }
  out_data <- out_data[,c(vec_names, "count")]
  return(out_data)
}


cbargy_multi_w <- function(data = data.frame(), vec_names = c(), weight_temp = c(), sepper = "lulululu"){
  new_vec <- rep("", dim(data)[1])
  for(i in 1:length(vec_names)){
    new_vec <- paste(new_vec, data[,vec_names[i]], sep = sepper)
  }
  out_data <- count_bar_generate_y_weighted(vec_temp = new_vec, weight_temp = weight_temp)
  for(i in 1:length(vec_names)){
    out_data[,vec_names[i]] <- ""
  }
  for(i in 1:dim(out_data)[1]){
    temp <- str_split(out_data$types[i], pattern = sepper)[[1]]
    for(j in 1:length(vec_names)){
      out_data[i,vec_names[j]] <- temp[j + 1]
    }
  }
  out_data <- out_data[,c(vec_names, "count")]
  return(out_data)
}


#### Test Area ####

# count_bar_generate_y(c(rep("立项", 3), rep("伦理", 4)))
# vec_1 <- sample(c("gege", "jiejie"), 100, replace = T)
# vec_2 <- sample(c("baba", "mama"), 100, replace = T)
# vec_3 <- sample(c("S", "L", "G", "B", "T"), 100, replace = T)
# values <- rnorm(n = 100, mean = 7, sd = 4)
# data <- data.frame(vec_1, vec_2, vec_3, values)
# vec_names <- c("vec_1", "vec_2", "vec_3")
# weight_temp <- values
# mean_bar_generate_y(vec_temp = vec_3, num_temp = values)
